package com.alinu.workbench.web.controller;

import com.alinu.settings.domain.User;
import com.alinu.utils.DateTimeUtil;
import com.alinu.utils.PrintJson;
import com.alinu.utils.ServiceFactory;
import com.alinu.utils.UUIDUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.Activity;
import com.alinu.workbench.domain.Clue;
import com.alinu.workbench.domain.Tran;
import com.alinu.workbench.service.ActivityService;
import com.alinu.workbench.service.ClueService;
import com.alinu.workbench.service.TranService;
import com.alinu.workbench.service.impl.ActivityServiceImpl;
import com.alinu.workbench.service.impl.ClueServiceImpl;
import com.alinu.workbench.service.impl.TranServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/18
 * @description 线索控制器类
 */
public class ClueController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        assert path != null;
        switch (path) {
            case "/workbench/clue/saveClue.do":
                saveClue(request, response);
                break;
            case "/workbench/clue/getClueList.do":
                getClueList(request, response);
                break;
            case "/workbench/clue/findClueById.do":
                findClueById(request, response);
                break;
            case "/workbench/clue/getRelatedActivities.do":
                getRelatedActivities(request, response);
                break;
            case "/workbench/clue/disassociation.do":
                disassociation(request, response);
                break;
            case "/workbench/clue/getActivitiesByName.do":
                getActivitiesByName(request, response);
                break;
            case "/workbench/clue/saveRelation.do":
                saveRelation(request, response);
                break;
            case "/workbench/clue/convertClue.do":
                convertClue(request, response);
                break;
            case "/workbench/clue/createSimpleTransaction.do":
                createSimpleTransaction(request, response);
                break;
            case "/workbench/clue/deleteClue.do":
                deleteClue(request, response);
                break;
            default:
                break;
        }
    }

    private void deleteClue(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        String clueId = request.getParameter("clueId");

        boolean result = clueService.deleteClue(clueId);

        PrintJson.printJsonResult(response, result);

    }

    private void convertClue(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        String clueId = request.getParameter("clueId");

        boolean result = clueService.convertClue(clueId);

        PrintJson.printJsonResult(response, result);


    }

    private void createSimpleTransaction(HttpServletRequest request, HttpServletResponse response) {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());
        Tran tran = new Tran();
        String money = request.getParameter("money");
        String name = request.getParameter("name");
        String expectedDate = request.getParameter("expectedDate");
        String stage = request.getParameter("stage");
        String activityId = request.getParameter("activityId");
        String createdBy = ((User) request.getSession().getAttribute("user")).getName();
        String source = request.getParameter("source");

        tran.setActivityId(activityId);
        tran.setCreateTime(DateTimeUtil.getSysTime());
        tran.setCreateBy(createdBy);
        tran.setExpectedDate(expectedDate);
        tran.setId(UUIDUtil.getUUID());
        tran.setMoney(money);
        tran.setName(name);
        tran.setStage(stage);
        tran.setSource(source);

        boolean result = tranService.createSimpleTransaction(tran);

        PrintJson.printJsonResult(response, result);


    }

    private void saveRelation(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        String clueId = request.getParameter("clueId");
        String activityId = request.getParameter("activityId");
        String[] activityIds = activityId.split(",");


        boolean result = clueService.saveRelation(clueId, activityIds);

        PrintJson.printJsonResult(response, result);


    }

    private void getActivitiesByName(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        Map<String, Object> params = new HashMap<>();

        params.put("clueId", request.getParameter("clueId"));
        params.put("name", request.getParameter("name"));
        params.put("activityId", request.getParameter("activityId"));

        List<Activity> activityList = activityService.getActivitiesByName(params);

        PrintJson.printJsonObj(response, activityList);

    }

    private void disassociation(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        String carId = request.getParameter("carId");

        boolean result = clueService.disassocaition(carId);

        PrintJson.printJsonResult(response, result);

    }

    private void getRelatedActivities(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        String clueId = request.getParameter("clueId");

        List<Activity> activityList = activityService.findActivitiesByClueId(clueId);

        PrintJson.printJsonObj(response, activityList);

    }

    private void findClueById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        String id = request.getParameter("id");

        Clue clue = clueService.findClueById(id);

        request.setAttribute("clue", clue);

        request.getRequestDispatcher("/workbench/clue/detail.jsp").forward(request, response);


    }

    private void getClueList(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        int start = (pageNo - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        params.put("start", start);
        params.put("pageSize", pageSize);
        params.put("fullname", request.getParameter("fullname"));
        params.put("owner", request.getParameter("owner"));
        params.put("company", request.getParameter("company"));
        params.put("mphone", request.getParameter("mphone"));
        params.put("phone", request.getParameter("phone"));
        params.put("source", request.getParameter("source"));
        params.put("state", request.getParameter("state"));

        PaginationVO<Clue> clueVO = clueService.getClueList(params);
        PrintJson.printJsonObj(response, clueVO);


    }

    private void saveClue(HttpServletRequest request, HttpServletResponse response) {
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        String fullname = request.getParameter("fullname");
        String appellation = request.getParameter("appellation");
        String owner = request.getParameter("owner");
        String company = request.getParameter("company");
        String job = request.getParameter("job");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String website = request.getParameter("website");
        String mphone = request.getParameter("mphone");
        String state = request.getParameter("state");
        String source = request.getParameter("source");
        String description = request.getParameter("description");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactTime = request.getParameter("nextContactTime");
        String address = request.getParameter("address");
        String id = UUIDUtil.getUUID();
        String createBy = ((User) request.getSession().getAttribute("user")).getName();
        String createTime = DateTimeUtil.getSysTime();

        Clue clue = new Clue();
        clue.setAddress(address);
        clue.setAppellation(appellation);
        clue.setCompany(company);
        clue.setContactSummary(contactSummary);
        clue.setCreateBy(createBy);
        clue.setCreateTime(createTime);
        clue.setDescription(description);
        clue.setEmail(email);
        clue.setFullname(fullname);
        clue.setId(id);
        clue.setJob(job);
        clue.setMphone(mphone);
        clue.setWebsite(website);
        clue.setSource(source);
        clue.setNextContactTime(nextContactTime);
        clue.setOwner(owner);
        clue.setPhone(phone);
        clue.setState(state);

        boolean result = clueService.saveClue(clue);

        PrintJson.printJsonResult(response, result);
    }


}
